package cn.tp.ksoft.civil_driver.module.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;

import cn.tp.ksoft.civil_driver.module.order.fragment.AllOrderFragment;
import cn.tp.ksoft.civil_driver.module.order.fragment.FinishOrderFragment;
import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.module.order.fragment.BaseOrderFragment;
import cn.tp.ksoft.civil_driver.module.order.fragment.ReceiptOrderFragment;
import cn.tp.ksoft.civil_driver.module.order.fragment.WaitOrderFragment;
import cn.tp.ksoft.civil_driver.view.widget.tablayou.ITabLayout;

/**
 * Created by zhusong on 16/5/2.
 */
public class OrdersActivity extends DriverBaseActivity {

    private LayoutInflater mInflater;
    private SparseArray<String> mTypes;
    private SparseArray<BaseOrderFragment> mFragments;
    private final int INDEX_ALL = 0;
    private final int INDEX_WAIT_ORDER = 1;
    private final int INDEX_ACCEPT_ORDER = 2;
    private final int INDEX_FINISH_ORDER = 3;
//    private SparseArray<RecyclerView> mContent;
//    private SparseArray<Boolean> mIsLoadingArr;
    private ITabLayout mTabs;
    private ViewPager mVp;
    private OrderPagerAdapter mAdapter;
//    private DriverOrderRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_orders);
        updateTitle(getString(R.string.driver_order));
        mTypes = new SparseArray<>(4);
        mTypes.append(0, "全部");
        mTypes.append(1, "待接单");
        mTypes.append(2, "已接单");
        mTypes.append(3, "已完成");
        mFragments = new SparseArray<>();
        mFragments.append(INDEX_ALL, AllOrderFragment.newInstance(INDEX_ALL));
        mFragments.append(INDEX_WAIT_ORDER, WaitOrderFragment.newInstance(INDEX_WAIT_ORDER));
        mFragments.append(INDEX_ACCEPT_ORDER, ReceiptOrderFragment.newInstance(INDEX_ACCEPT_ORDER));
        mFragments.append(INDEX_FINISH_ORDER, FinishOrderFragment.newInstance(INDEX_FINISH_ORDER));
        mTabs = (ITabLayout) findViewById(R.id.tabs);
        mVp = (ViewPager) findViewById(R.id.vp);
        mVp.setOffscreenPageLimit(4);
        mAdapter = new OrderPagerAdapter(getSupportFragmentManager());
        mVp.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mVp);
    }

    class OrderPagerAdapter extends FragmentPagerAdapter {

        public OrderPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return null != mTypes ? mTypes.size() : 0;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTypes.get(position);
        }
    }
}
